package com.example.springboot1.utils.exception;

import com.example.springboot1.utils.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @aythor Tian
 * @date 2022/5/19-19:30
 **/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    //自定义异常
    @ExceptionHandler(MyException.class)
    @ResponseBody //为了返回数据
    public Result error(MyException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMsg());
    }
}
